package com.kobeliu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.enums.YesOrNo;
import com.kobeliu.exceptions.GraceException;
import com.kobeliu.mapper.DataDictionaryMapper;
import com.kobeliu.pojo.DataDictionary;
import com.kobeliu.pojo.bo.DataDictionaryBO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.service.DataDictService;
import com.kobeliu.utils.PagedGridResult;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Dictionary;
import java.util.List;

@Service
public class DataDictServiceImpl extends BaseInfoProperties implements DataDictService {

    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    @Transactional
    @Override
    public void createOrUpdateDataDict(DataDictionaryBO bo) {


        DataDictionary dataDictionary = new DataDictionary();
        BeanUtils.copyProperties(bo,dataDictionary);

        if(StringUtils.isBlank(bo.getId())){
            //创建

            //判断数据字典项是不可重复的
            DataDictionary isExist = dataDictionaryMapper.selectOne(new QueryWrapper<DataDictionary>()
                                        .eq("item_key",bo.getItemKey())
                                        .eq("item_value",bo.getItemValue()));

            if(isExist!=null){
                GraceException.display(ResponseStatusEnum.DATA_DICT_EXIST_ERROR);
            }else{
                dataDictionaryMapper.insert(dataDictionary);
            }
        }else{
            //更新
            dataDictionaryMapper.updateById(dataDictionary);
        }
    }

    @Override
    public PagedGridResult dataDictList(String typeName, String itemValue, Integer page, Integer limit) {

        PageHelper.startPage(page,limit);

        List<DataDictionary> dictList = dataDictionaryMapper.selectList(new QueryWrapper<DataDictionary>()
                .like("type_name",typeName)
                .like("item_value",itemValue)
                .orderByAsc("type_code")
                .orderByAsc("sort"));


        return setterPagedGrid(dictList,page);

    }

    @Override
    public DataDictionary getDataDict(String dictId) {
        return dataDictionaryMapper.selectById(dictId);
    }

    @Transactional
    @Override
    public void delDataDict(String dictId) {
        Integer res = dataDictionaryMapper.delete(new QueryWrapper<DataDictionary>()
                .eq("id",dictId));

        if(res==0){
            GraceException.display(ResponseStatusEnum.DATA_DICT_DELETE_ERROR);
        }
    }

    @Override
    public List<DataDictionary> getDataByCode(String typeCode) {
        return dataDictionaryMapper.selectList(new QueryWrapper<DataDictionary>()
                .eq("type_code",typeCode)
                .eq("enable", YesOrNo.YES.type)
                .orderByAsc("sort"));
    }

    @Override
    public List<DataDictionary> selectItemsByKeys(String... keys) {
        List<DataDictionary> ddList = dataDictionaryMapper.selectList(
                new QueryWrapper<DataDictionary>()
                        .eq("enable",YesOrNo.YES.type)
                        .in("item_key",keys)
        );
        return ddList;
    }
}
